<?php
/**
 * common model file Created by PhpStorm.
 * User: yang
 * Date: 2020/06/11
 */

namespace Common\Model;

class User extends Base
{
    protected $table = 'users';
    protected $guarded = [];

    //public $timestamps = false;

    //一对多关联评论
    public function get_comment()
    {
        return $this->hasMany(ForumComment::class, 'user_id', 'id')
            ->select(['id', 'user_id']);
    }

    //一对多关联点赞
    public function get_zan()
    {
        return $this->hasMany(ForumZan::class, 'user_id', 'id')
            ->select(['id', 'user_id']);
    }

    /**
     * 获取图片可用链接
     * @param $value
     * @return array
     */
    public function getHeadImgAttribute($value)
    {
        if (strlen($value) > 10) {
            $url = oss_url($value) . '?x-oss-process=image/resize,m_fill,h_200,w_200';
        } else {
            $url = "/static/web/res/images/avatar/" . $value . ".jpg";
        }

        $arr_data = [
            'src' => $value,
            'url' => $url,
        ];
        return $arr_data;
    }

}